/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *
 */
package org.mizartools.dli;

public class ExistentialRegistration extends ItemDefinition {

	private Loci loci = null;
	private Cluster cluster = null;
	private Type type = null;
	
	private ExistentialRegistration(){
		super(null);
	}

    public ExistentialRegistration(Loci loci, Cluster cluster, Type type) throws DliException {
    	super(null);
		if (loci == null){
			throw new DliException();
		}
		this.loci = loci;
		this.type = type;
		this.cluster = cluster;
    }
	
	public static ItemDefinition newInstance(String string) throws DliParseException {
		ExistentialRegistration existentialRegistration = new ExistentialRegistration();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in ExistentialRegistration");
		}
		if (!string.substring(0, position).equals("exreg")) {
			throw new DliParseException("["+string+"] is not valid in ExistentialRegistration");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in ExistentialRegistration");
		}
		String type = null;
		while (positionComma != -1) {
			position = string.indexOf("(");
			int positionCommaTest = string.indexOf(",");
			if (positionCommaTest != 0 && positionComma<position){
				position = positionCommaTest;
			}
			if (position == -1) {
				throw new DliParseException("["+string+"] is not valid in ExistentialRegistration");
			}
			type = string.substring(0, position);
			if (type.equals("$loci")){
				existentialRegistration.loci = Loci.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$cluster")){
				existentialRegistration.cluster = Cluster.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$type")){
				existentialRegistration.type = Type.newInstance(string.substring(0, positionComma));
			} else {
				throw new DliParseException("["+string+"] is not valid in ExistentialRegistration");
			}
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		position = string.indexOf("(");
		type = string.substring(0, position);
		if (type.equals("$loci")){
			existentialRegistration.loci = Loci.newInstance(string);
		} else if (type.equals("$cluster")){
			existentialRegistration.cluster = Cluster.newInstance(string);
		} else if (type.equals("$type")){
			existentialRegistration.type = Type.newInstance(string);
		} else {
			throw new DliParseException("["+string+"] is not valid in ExistentialRegistration");
		}
		return existentialRegistration;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("exreg(");
		sb.append(loci.toString());
		if (cluster != null){
			sb.append(",");
			sb.append(cluster.toString());
		}
		if (type != null){
			sb.append(",");
			sb.append(type.toString());
		}
		sb.append(")");
		return sb.toString();
		
	}
	
}
